﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

using Iservices;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Model;
using EFEntity;
using AutoMapper;

namespace Services
{
    public class config_public_charServices : Iconfig_public_charServices
    {
        private readonly MyDbContext myDbContext;
        private readonly IMapper mapper;
        public config_public_charServices(MyDbContext myDbContext, IMapper mapper)
        {
            this.myDbContext = myDbContext;
            this.mapper = mapper;
        }
        public async Task<int> DelTest(int id)
        {
            int result = 0;
            Econfig_public_char s = myDbContext.Tests.Find(id);
            myDbContext.Tests.Remove(s);
            result = await myDbContext.SaveChangesAsync();
            return result;
           
        }

        public async Task<int> TestAdd(config_public_char p)
        {
           
            Econfig_public_char etest = mapper.Map<Econfig_public_char>(p);
            myDbContext.Tests.Add(etest);
            int reslut = await myDbContext.SaveChangesAsync();
            return reslut;
        }

        public async Task <List<config_public_char>> TestSelect()
        {
           // List<Econfig_public_char> list = await myDbContext.Tests.FromSqlRaw("select  Id, attribute_kind, attribute_name from dbo.config_public_char").ToListAsync();


            List<Econfig_public_char> list = new List<Econfig_public_char>();
            await Task.Run(() => {
                list = myDbContext.Tests.ToList();
            });

            //把EO-DTO
            List<config_public_char> list2 = mapper.Map<List<config_public_char>>(list);
            return list2;
        }

    }
}
